package presstrack
{
	import flash.net.registerClassAlias;
	import flash.net.Responder;	
	import mx.utils.ObjectUtil;	
	import org.presstrack.service.*;
	
	[RemoteClass(alias="presstrack.ProductClass")]
	
	[Bindable]
	public class ProductClass
	{
		public var idproduct:int;
  		
  		public var label:String;
  		public var run:int;
  		public var size:String;
  		public var color:String;
  		public var group:int;  		
  		public var order:int;
  						
		public function ProductClass(product:Object = null) 
		{
			if (product != null) setFrom(product);
		}
		
		public function setFrom(ob:Object):void{
			if (ob.hasOwnProperty("idproduct"))
				idproduct = Number(ob.idproduct);
				
			if (ob.hasOwnProperty("run"))
				run = Number(ob.run);
				
			if (ob.hasOwnProperty("label"))
				label = ob.label.toString();
				
			if (ob.hasOwnProperty("size"))
				size = ob.size.toString();
				
			if (ob.hasOwnProperty("color"))
				color = ob.color.toString();
				
			if (ob.hasOwnProperty("group"))
				group = Number(ob.group);
				
			if (ob.hasOwnProperty("order"))
				order = Number(ob.order);
		}
		
		public function setLabel(value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("label", value, resFunc, faultFunc);
		}
		
		public function setRun(value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("run", value, resFunc, faultFunc);
		}
		
		public function setParams(params:Array, resFunc:Function = null, faultFunc:Function = null):void {
			if(params["label"] != label)
				updateParam("label", params["label"], resFunc, faultFunc);
			if(params["run"] != run)
				updateParam("run", params["run"], resFunc, faultFunc);
			if(params["size"] != size)
				updateParam("size", params["size"], resFunc, faultFunc);
			if(params["color"] != color)
				updateParam("color", params["color"], resFunc, faultFunc);
		}
		
		// private
		
		private function updateParam(param:String, value:String, resFunc:Function = null, faultFunc:Function = null) : void 
		{
			CrudService.crud.srv.call("PressTrack.UpdateProduct", new Responder( onResult, onFault ), idproduct, param, value);
			
			function onResult(res:String) : void {
				if (res == 'successful') {
					if (param == "label")
						label = value;
					else if (param == "color")
						color = value;
					else if (param == "size")
						size = value;
					else if (param == "run")
						run = int(value);
					if (resFunc != null) resFunc();
				}
				else
					faultFunc(res);	
			}
			
			function onFault(e:Object) : void {
				faultFunc(ObjectUtil.toString(e));
			}
		}
	}
}